#include "main_head.h"
#include"head\Pipe.h"
#include<stdio.h>
#include<cstdlib>

Pipe::Pipe(GLint _program) {
	Program = _program;
	for (int i = 0; i < 6; i++)
	{
		srand((unsigned int)(glfwGetTime()*rand() * 1000));
		pipePositions[i].y = ((rand() % 16) - 7)*0.05f;
	}
}

void Pipe::change_model(GLint which) {
	glm::mat4 model;
	model = glm::translate(model, pipePositions[which]);
	GLint viewLoc = glGetUniformLocation(Program, "model");
	glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(model));
}

void Pipe::move() {
	if (!live)return;
	control_all++;
	control++;
	if (control % N == 0)
	{
		control = 0;
		N = 1200;
		srand((unsigned int)glfwGetTime()*rand());
		if (which != 0)
		{
			pipePositions[which].x = pipePositions[which - 1].x + 0.6f;
			pipePositions[which].y = ((rand() % 16) - 7)*0.05f;
			which++;
			which %= 6;
		}
		else
		{
			pipePositions[which].x = pipePositions[5].x + 0.6f;
			pipePositions[which].y = ((rand() % 16) - 7)*0.05f;
			which++;
		}
	}

	view = glm::translate(view, glm::vec3(-0.0005, 0.0f, 0.0f));
	GLint viewLoc = glGetUniformLocation(Program, "view");
	glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
}

void Pipe::load_image() {
	GLint width, height;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture); // All upcoming GL_TEXTURE_2D operations now have effect on our texture object
	// Set our texture parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// Set texture wrapping to GL_REPEAT
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// Set texture filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	// Load, create texture and generate mipmaps

	unsigned char* image = SOIL_load_image("picture/pipe.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	glGenerateMipmap(GL_TEXTURE_2D);
	SOIL_free_image_data(image);
	glBindTexture(GL_TEXTURE_2D, 0);
}

void Pipe::load_coord() {
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);

	glBindVertexArray(VAO);

	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(position), position, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
	glEnableVertexAttribArray(1);
}

void Pipe::draw(GLboolean start) {
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texture);

	glUniform1i(glGetUniformLocation(Program, "ourTexture"), 0);
	glBindVertexArray(VAO);
	
	for (int i = 0; i < 6; i++)
	{
		change_model(i);
		glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
	}
	if(start)move();
	
	glBindVertexArray(0);
}

void Pipe::burn() {
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);
}

void Pipe::set_live(GLint _live) { live = _live; }
GLint Pipe::_control_all() { return control_all; }
